package com.three.box.activity

import android.content.Intent
import android.os.Bundle
import com.hjq.toast.Toaster
import com.three.box.base.BaseActivity
import com.three.box.databinding.ActivityAddProductBinding
import com.three.box.entity.Product
import com.three.box.entity.ProductConfig
import com.three.box.network.AppRepository
import com.three.box.network.NetworkUtil
import com.three.box.widget.ProductConfigAdapter

class AddProductActivity : BaseActivity() {

    companion object {
        const val TAG = "AddProductActivity"
    }

    private lateinit var binding: ActivityAddProductBinding

    private lateinit var adapter: ProductConfigAdapter
    private val productConfigList = mutableListOf<ProductConfig>() // 列表当前数据
    private lateinit var existProductIds: ArrayList<Int>

    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityAddProductBinding.inflate(layoutInflater)
        setStatusBar()
        super.onCreate(savedInstanceState)

        existProductIds = intent.getIntegerArrayListExtra("product_ids") ?: ArrayList()
        initView()

        initValues()
    }

    override fun getLayoutView() = binding.root

    private fun initView() {
        binding.backIv.setOnClickListener { finish() }
        binding.syncTv.setOnClickListener {
            if (!NetworkUtil.isNetworkAvailable(this)) {
                Toaster.show("网络错误，请检查后重试")
                return@setOnClickListener
            }
            AppRepository.instance.configInfo { code, msg ->
                if (code < 0) {
                    Toaster.show(msg)
                    return@configInfo
                }
                initValues()
            }
        }
        binding.confirmBtn.setOnClickListener {
            val checkedList = ArrayList<Product>()
            val groupList = AppRepository.instance.productGroupList
            val group = groupList[0]
            productConfigList.forEach { productConfig ->
                val modelList = productConfig.productModels
                modelList.forEach { productModel ->
                    if (productModel.checked) {
                        checkedList.add(Product(
                            productId = productConfig.id,
                            productName = productConfig.name,
                            productDesc = productConfig.desc,
                            productModelId = productModel.id,
                            number = 1,
                            groupId = group.id,
                            groupName = group.name,
                            productModelName = productModel.name
                        ))
                    }
                }
            }
            setResult(RESULT_OK, Intent().apply {
                putParcelableArrayListExtra("product_list", checkedList)
            })
            finish()
        }

        adapter = ProductConfigAdapter(this, productConfigList)
        binding.expandableListView.setAdapter(adapter)
        binding.expandableListView.setOnGroupClickListener { parent, _, groupPosition, _ ->
            if (parent.isGroupExpanded(groupPosition)) {
                parent.collapseGroup(groupPosition)
            } else {
                parent.expandGroup(groupPosition)
            }
            true
        }
    }

    private fun initValues() {
        productConfigList.addAll(AppRepository.instance.productConfigList)
        adapter.notifyDataSetChanged()
    }
}